package com.gark.pieceswallpaper;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.renderscript.RenderScript;
import android.renderscript.RenderScriptGL;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.widget.Toast;

import java.io.IOException;

public class MyWallpaperService extends WallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener {

    private SharedPreferences pref;
    private Engine engine;

//    @Override
//    public boolean onUnbind(Intent intent) {
//        Toast.makeText(MyWallpaperService.this.getApplicationContext(), "Toast = ", Toast.LENGTH_SHORT).show();
//        return super.onUnbind(intent);
//    }

    @Override
    public Engine onCreateEngine() {
//        SharedPreferences pref;
//        pref = this.getSharedPreferences(PREFERENCES, 0);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        pref.registerOnSharedPreferenceChangeListener(MyWallpaperService.this);
        engine = new FallingSnowWallpaperEngine();
        return engine;
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        if (getString(R.string.count_settings).equals(s)) {


            ((FallingSnowWallpaperEngine) engine).reInit();

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
            try {
                wallpaperManager.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    private class FallingSnowWallpaperEngine extends Engine {
        private RenderScriptGL mRenderScriptGL;
        private GravityRS mSnowRS;


        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            surfaceHolder.setSizeFromLayout();

        }


        @Override
        public void onDestroy() {
            super.onDestroy();
            pref.unregisterOnSharedPreferenceChangeListener(MyWallpaperService.this);
            if (mSnowRS != null) {
//				mSnowRS.stop();
                mSnowRS = null;
            }
            if (mRenderScriptGL != null) {
                mRenderScriptGL.destroy();
                mRenderScriptGL = null;
            }
        }


        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);

            if (mRenderScriptGL != null) {
                mRenderScriptGL.setSurface(holder, width, height);
            }
            if (mSnowRS == null) {
                mSnowRS = new GravityRS();
                mSnowRS.init(mRenderScriptGL, getResources(), width, height, MyWallpaperService.this);
            }
        }

        public void reInit() {
            try {
                mSnowRS = new GravityRS();
                mSnowRS.init(mRenderScriptGL, getResources(), 0, 0, MyWallpaperService.this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);

            RenderScriptGL.SurfaceConfig surfaceConfig = new RenderScriptGL.SurfaceConfig();
            mRenderScriptGL = new RenderScriptGL(MyWallpaperService.this, surfaceConfig);

            // use low for wallpapers
            mRenderScriptGL.setPriority(RenderScript.Priority.LOW);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            if (mSnowRS != null) {
                mSnowRS = null;
            }
            if (mRenderScriptGL != null) {
                mRenderScriptGL.destroy();
                mRenderScriptGL = null;
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            if (mSnowRS != null) {
                if (visible) {
                    mSnowRS.resume();
                } else {
                    mSnowRS.pause();
                }
            }
        }

        @Override
        public void onTouchEvent(MotionEvent ev) {
            super.onTouchEvent(ev);
            mSnowRS.newTouchPosition(ev.getX(0), ev.getY(0), ev.getPressure(0), ev.getPointerId(0));
        }

    }

}
